<?php
namespace app\index\controller;
use AlipayService;
use WxpayService;
use think\Config;
use think\Db;

class Payreturn
{
    public function aliPayReturn()
    {
//        验签
//        $aop = new \AopClient();
//        $aop->alipayrsaPublicKey = \Comm\Pay\Alipay::ALIPAY_RSA_PUBLIC_KEY;
//        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        if(isset($_POST['trade_status'])|| isset($_POST['trade_status'])){
            if($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED') {
                //开始请求对应项目的接口
                $out_trade_no = $_POST['out_trade_no'];//支付宝返回的交易号
                $pay_re = Db::table('ims_zh_pay_order')->where(array('out_trade_no'=>$out_trade_no))->find();//查询订单记录 1232113233
                if($pay_re){
                    $url=config('source.'.$pay_re['source']);
                    $re=$this->request_post($url,array('code'=>'1000','out_trade_no'=>$out_trade_no,'trade_status'=>$_POST['trade_status'],'total_amount'=>$_POST['total_amount'],'pay_ment'=>'1'));
                    $reNew=json_decode($re,true);
                    if($reNew['code'] == 1001){
                        echo "success";
                        exit();
                    }
                }
            }
        }
        echo "fail";
        exit();
    }


    public function wxPayReturn()
    {
        $testxml  = file_get_contents("php://input");
//将xml转化为json格式
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
//转成数组
        $result = json_decode($jsonxml, true);
        if($result){
            //如果成功返回了
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
                $pay_re = Db::table('ims_zh_pay_order')->where(array('out_trade_no'=>$result['out_trade_no']))->find();//查询订单记录
                if(!empty($pay_re)){
                    $url=config('source.'.$pay_re['source']);
                    $rearr=array('code'=>'1000','out_trade_no'=>$result['out_trade_no'],'trade_status'=>$result['return_code'],'total_amount'=>$result['total_fee']/100,'pay_ment'=>'2');
                    $re=$this->request_post($url,$rearr);
                    $reNew=json_decode($re,true);
                    if($reNew['code'] == 1001){
                        echo "success";
                        exit();
                    }
                }
            }
            Db::table('ims_zh_pay_order')->where(array('out_trade_no'=>$result['out_trade_no']))->update(array('wechat_trade_status'=>'err'));//更新订单状态
        }
    }


    /**
     * 模拟post进行url请求
     * @param string $url
     * @param string $param
     */
    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}
